/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Units.attacks;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import mygame.Units.Ally;
import mygame.Units.Enemy;
import mygame.Units.Unit;
import mygame.World;

/**
 *
 * @author Ricardo
 */
public abstract class Weapon {
    
    public static enum AttackType{INSTANT,CHANNELED,PROJECTIL};
    public static enum DamageType{PHYSICAL,MAGIC,FIRE};
    
    protected float range;
    protected AttackType attackType;
    protected DamageType damageType;
    protected float damage;
    protected float projectilSpeed;
    protected float reloadTime;
    
    protected boolean attackself;
    protected boolean attackPlayer,attackAlly,attackEnemy;
    
    protected Vector3f locationOffset;
    protected Unit owner;

    public Weapon(float range, AttackType type, DamageType damageType, float damage, float projectilSpeed, float reloadTime) {
        this.range = range;
        this.attackType = type;
        this.damageType = damageType;
        this.damage = damage;
        this.projectilSpeed = projectilSpeed;
        this.reloadTime = reloadTime;
    }
    
    
    
    abstract public void build(AssetManager assetManager, PhysicsSpace physicsSpace, Node rootNode, Node shootables);
    
    abstract public void update(float tpf);
    
    public void attach(Unit unit, Vector3f locationOffset, boolean attackPlayer, boolean attackAlly, boolean attackEnemy){
        this.owner = unit;
        this.locationOffset = locationOffset;
        this.attackPlayer = attackPlayer;
        this.attackAlly = attackAlly;
        this.attackEnemy = attackEnemy;
    }
    
    protected Unit findClosestTarget(Vector3f firingLocation){
        Unit closest=null;
        float closestDistance=999999f;
        float distance;
        if(attackPlayer){
            distance = firingLocation.distanceSquared(World.player.getLocation());
            if(distance<range && distance<closestDistance){
                closest = World.player;
                closestDistance=distance;
            }
        }else if(attackAlly){
            for (Ally ally : World.allies) {
                distance = firingLocation.distanceSquared(ally.getLocation());
                if(distance<range && distance<closestDistance){
                    closest = ally;
                    closestDistance=distance;
                }
            }
        }else if(attackEnemy){
            for (Enemy enemy : World.enemies) {
                distance = firingLocation.distanceSquared(enemy.getLocation());
                if(distance<range && distance<closestDistance){
                    closest = enemy;
                    closestDistance=distance;
                }
            }
        }
        return closest;
    }
}
